package com.akka.akkatry;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author Dmitry
 */
public class SocketServer {
    public static final byte[] intToByteArray(int value, byte[] buffer) {
        buffer[0] = (byte) (value >>> 24);
        buffer[1] = (byte) (value >>> 16);
        buffer[2] = (byte) (value >>> 8);
        buffer[3] = (byte) (value);
        return buffer;
    }

    public static void main(String[] args) throws IOException {
        System.out.println("Server Started");
        ServerSocket ssocket = new ServerSocket(8007);
        Socket socket = ssocket.accept();
        System.out.println("Server accepted client");
        socket.setPerformancePreferences(0, 1, 2);
        socket.setSendBufferSize(2048);
        socket.setReceiveBufferSize(2048);
        socket.setTcpNoDelay(true);
        OutputStream outputStream = new BufferedOutputStream(socket.getOutputStream(), 2048);
        String str = "my big value string afafdfddd lkjdlk jdsaflkjadflkjsadfklj sdflkjs dfklj sfd;lkj sdflkj dflksj lsdkfjsl;k fjsaldkfjslakfj asldkfjsa;lkdfj slkdafjs ldkfjsldkfjlsakdjflskdjflskjdfslkdfjslkdfjslkdjfslakjdf12345uiokjh kjhkjhkjhlkjhk jhl kjh lkjh lkjh 67890kjflkjsdafr lkjsdfalkjsfdlksjfdlkasjdf;lkjsdflkjsafdlkjsafdlkjsfldkjasdlfkjaslkrjwrl4kjsdflkjsdflkjsfdlkjsfdlkjsdflkjsfd lksjdfl ksjdflkjsadflkjsdflksjfd;lkj][poiuytkljlkj lkjlkjlkjlkjlkjlkjlkjlkjlkjlkjlkjlkjlkjlkjlkjrewqasdfghjkl;'";
        str = str + str + str + str;
        byte[] buffer = str.getBytes();
        byte[] length = new byte[4];
        intToByteArray(buffer.length, length);
        while (true) {
            outputStream.write(length);
            outputStream.write(buffer);
            // outputStream.flush();
        }
    }
}
